<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>获取元素</title>
</head>

<body>
    <!--  1. 根据ID获取元素 -->
    <div id="tim">2022-4-30</div>
    <script>
        // 文档页面从上往下执行，要先有标签，script写在标签的下面
        document.getElementById('tim');
        console.log(tim);
        // 返回一个元素对象
    </script>

    <!--  2.根据标签名获取元素 -->
    <ul>
        <li>巢彩靖是大美女</li>
        <li>巢彩靖是大美女</li>
        <li>巢彩靖是大美女</li>
        <li>巢彩靖是大美女</li>
        <li>巢彩靖是大美女</li>
    </ul>
    <ol id="ol">
        <li>wje真好看</li>
        <li>wje真好看</li>
        <li>wje真好看</li>
    </ol>
    <script>
        var lis = document.getElementsByTagName('li');
        console.log(lis);
        // 返回获取过来的元素对象的集合
        console.log(lis[0]);
        // 获取某个父元素内部的指定的子标签(不包括父元素) element.getElementsByTagName('标签名')
        /* var ol = document.getElementsByTagName('ol');
        console.log(ol[0].getElementsByTagName('li')); */
        var ol = document.getElementById('ol');
        console.log(ol.getElementsByTagName('li'));
    </script>

    <!-- 3.根据就类名获 -->
    <div class="box">盒子1</div>
    <div class="box">盒子2</div>
    <div id="nav">
        <ul>
            <li>首页</li>
            <li>产品</li>
        </ul>
    </div>
    <script>
        // getElementsByClassName()方法  
        var boxes = document.getElementsByClassName('box');
        console.log(boxes);
        // querySelector()方法  返回选择器的第一个元素对象
        var firstBox = document.querySelector('.box');
        console.log(firstBox);
        var nav = document.querySelector('#nav');
        console.log(nav);
        var all = document.querySelectorAll('.box'); //得到所有的box
        console.log(all);
    </script>

    <!-- 4.获取特殊元素(body,html) -->
    <script>
        //获取body 
        var bodyele = document.body;
        console.log(bodyele);
        // 获取html
        var htmlele = document.documentElement;
        console.log(htmlele);
    </script>

</body>
</body>

</html>